package com.jichangxiu.framework.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.jichangxiu.common.annotation.Limit;
import com.jichangxiu.framework.utils.LimitUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LimitAspect {

    @Pointcut("@annotation(limit)")
    public void pointcutLimitAround(Limit limit) {
    }

    @Around(value = "pointcutLimitAround(limit)", argNames = "proceedingJoinPoint , limit")
    public Object limitAround(ProceedingJoinPoint proceedingJoinPoint, Limit limit) throws Throwable {
        if (ObjectUtil.isNotEmpty(limit) && limit.enabled())
            LimitUtils.setLimitRequest(proceedingJoinPoint, limit.second(), limit.count(), limit.limitType());
        return proceedingJoinPoint.proceed();
    }

}
